/**
 * city基础接口
 */
const AV = require("leanengine");
const _ = require("underscore");
const BaseService = require("./baseService");
class Service extends BaseService {
  constructor() {
    super("Film");
  }

  async getList(ctx) {
    let { pageSize = 10, pageNum = 1 } = ctx.params;
    let query = new AV.Query("Film");
    query.descending("updatedAt");
    query.limit(pageSize);
    query.skip(pageSize * pageNum - pageSize);
    try {
      let result = await query.find();
      let films = result.map(item => {
        let update = this.formatDate(item.get("updatedAt"));
        return {
          ...item.attributes,
          filmId: item.id,
          update
        };
      });
      let total = await query.count();
      return {
        // result,
        code: "666",
        msg: "ok",
        total,
        pageSize,
        pageNum,
        films
      };
    } catch (error) {
      return this.errInfo(error);
    }
  }

  async upload(ctx) {
    const file = ctx.request.files;
    try {
      let result = await this.uploadIns(file);
      let url = result.get("url");
      let imgId = result.id;
      return {
        code: "666",
        msg: "图片上传成功",
        image: {
          url,
          name: file.file.name,
          imgId
        }
      };
    } catch (error) {
      return this.errInfo(error);
    }
  }

  async checkName(ctx) {
    const params = ctx.params;
    try {
      let result = await this.checkNameIns(params.name);
      if (result.length > 0) {
        return {
          code: "101",
          msg: "电影名称已存在"
        };
      } else {
        return {
          code: "666",
          msg: "名称可以使用"
        };
      }
    } catch (error) {
      return this.errInfo(error);
    }
  }

  async add(ctx) {
    const params = ctx.params;
    let result = this.checkAttr(["name"], ctx.params);
    if (result) return result;
    result = await this.checkNameIns(params.name);
    if (result) return result;
    try {
      const film = new AV.Object("Film");
      for (let key in params) {
        film.set(key, params[key]);
      }

      let result = await film.save();
      return {
        code: "666",
        msg: "添加成功",
        result
      };
    } catch (error) {
      return this.errInfo(error);
    }
  }

  async edit(ctx) {
    const { filmId } = ctx.params;
    if (!filmId) {
      return {
        code: 601,
        msg: "缺少filmId"
      };
    }
    try {
      const entry = AV.Object.createWithoutData("Film", filmId);
      const params = ctx.params;
      for (let p in params) {
        entry.set(p, params[p]);
      }
      const result = await entry.save();
      return {
        code: "666",
        msg: "success",
        result
      };
    } catch (error) {
      console.log("error", error);
      return {
        code: error.code,
        msg: error.message
      };
    }
  }

  async delById(ctx) {
    const { filmId } = ctx.params;
    try {
      let entry = new AV.Object.createWithoutData("Film", filmId);
      let result = await entry.destroy();
      return {
        code: "666",
        msg: "删除成功"
      };
    } catch (error) {
      return this.errInfo(error);
    }
  }

  async getDetail(ctx) {
    const { filmId } = ctx.params;
    try {
      let query = new AV.Query("Film");

      let result = await query.get(filmId);
      return {
        code: "666",
        msg: "ok",
        film: {
          ...result.attributes,
          filmId: result.id
        }
      };
    } catch (error) {
      return this.errInfo(error);
    }
  }

  async findByCityId(ctx) {
    const { cityId = "5c67e4a144d90419c1ef3fa1" } = ctx.params;
    // 根据城市id查出所有区域
    const city = AV.Object.createWithoutData("City", cityId);
    var areaQuery = new AV.Query("Area");
    areaQuery.equalTo("dependent", city);
    const areas = await areaQuery.find();
    const areaIds = _.pluck(areas, "id");
    const query = new AV.Query("Film");
    query.include(["dependent"]);
    const result = await query.find();
    const res = result.filter((item, i) => {
      try {
        const areaId = item._serverData.dependent._serverData.dependent.id;
        return _.indexOf(areaIds, areaId) > -1;
      } catch (error) {
        return false;
      }
    });
    return {
      code: "666",
      msg: "success",
      films: res
    };
  }
}

const service = new Service();
module.exports = service;
